package com.zztl.infiniteBand.presenter

import android.content.Context
import android.text.TextUtils
import com.zztl.infiniteBand.R
import com.zztl.infiniteBand.model.NoDataResponseBean
import com.zztl.infiniteBand.net.FilterSubscriber
import com.zztl.infiniteBand.net.RetorfitServiceManger
import com.zztl.infiniteBand.net.converter.DataConverter
import com.zztl.infiniteBand.net.requestBean.RegisterRequest
import com.zztl.infiniteBand.ui.view.mvpview.RegisterView
import com.zztl.infiniteBand.util.UiUtils
import com.zztl.infiniteBand.util.toast


/**
 * @desc
 * @auth ${user}
 * @time 2018/6/11  18:27
 */
class AmendPwdPresenter: BaseCodePresenter<RegisterView>() {

    fun check(phoneNum: String, pwdNum: String, affitmPwdnum: String, codeNum: String): Boolean {
        if (TextUtils.isEmpty(phoneNum)) {
            UiUtils.context.toast(UiUtils.getString(R.string.手机号不能为空))
            return false
        }
        if (TextUtils.isEmpty(pwdNum)) {
            UiUtils.context.toast(UiUtils.getString(R.string.密码不能为空))
            return false
        }
        if (TextUtils.isEmpty(affitmPwdnum)) {
            UiUtils.context.toast(UiUtils.getString(R.string.密码不能为空))
            return false
        }
        if (TextUtils.isEmpty(codeNum)) {
            UiUtils.context.toast(UiUtils.getString(R.string.验证码不能为空))
            return false
        }

        if (pwdNum.length < 6 || pwdNum.length > 16) {
            UiUtils.context.toast(UiUtils.getString(R.string.密码长度为6到16位))
            return false
        }
        if (pwdNum != affitmPwdnum) {
            UiUtils.context.toast(UiUtils.getString(R.string.两次密码输入不一致))
            return false
        }
        return true
    }

    fun amendPwd(registerRequest: RegisterRequest, context: Context) {
        addSubscribe(RetorfitServiceManger.observe(RetorfitServiceManger.instance!!.apiService
                .amendPwd(DataConverter.instance.toJson(registerRequest)))
                .subscribe(object : FilterSubscriber<NoDataResponseBean>(context) {

                    override fun success(bean: NoDataResponseBean) {
                        view!!.registerSuccess()
                    }


                }))
    }
}
